package cn.bytest.netty.serialize;

import cn.bytets.rpc.api.code.ChannelByteBuf;
import io.netty.buffer.ByteBuf;

import java.io.IOException;
import java.io.OutputStream;

public class ChannelBufferOutput extends OutputStream {

    private final ChannelByteBuf buffer;

    private final int startIndex;

    public ChannelBufferOutput(ChannelByteBuf buffer){
        if (null==buffer){
            throw new NullPointerException("ByteBuf is Null");
        }
        this.buffer = buffer;
        startIndex = buffer.writerIndex();
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (len == 0) {
            return;
        }
        //buffer.writeInt(len-off);
        buffer.writeBytes(b, off, len);
    }

    @Override
    public void write(byte[] b) throws IOException {
        //buffer.writeInt(b.length);
        buffer.writeBytes(b);
    }

    @Override
    public void write(int b) throws IOException {
        buffer.writeByte((byte) b);
    }
    public ChannelByteBuf getBuffer(){
        return this.buffer;
    }
}
